package ferp.android.managers;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import ferp.android.R;
import ferp.android.activities.preferences.Preferences;
import ferp.android.tasks.center.TaskPollSubmit;
import ferp.poll.Option;
import ferp.poll.Poll;
import ferp.poll.Question;

/**
 * User: olegs
 * Date: 11/05/2014 14:29
 */
public class PollManager
{
  public static interface Listener
  {
    void onSubmitPollResultsStarted();
    void onSubmitPollResultsEnded(boolean success, int bonus);
  }

  // get last seen poll ID
  public static long getLastPollID(SharedPreferences sp)
  {
/*
    if (BuildConfig.DEBUG)
    {
      SharedPreferences.Editor editor = sp.edit();

      editor.putLong(LAST_ID, 0);
      editor.commit();
    }
*/

    return sp.getLong(PollManager.LAST_ID, 0);
  }

  // set current poll
  public static void set(Resources resources, Poll poll)
  {
    PollManager.poll = poll;

    if (poll != null)
    {
      // standard zero question
      PollManager.q0 = new Question(poll.description(),
                                    new Option(resources.getString(R.string.poll_option_start) + "<br><i><font color=\"green\">" + resources.getString(R.string.poll_bonus) + ": " + poll.bonus().description() + "</font></i>"),
                                    new Option(resources.getString(R.string.poll_option_postpone)),
                                    new Option(resources.getString(R.string.poll_option_refuse)));
    }
  }

  // get current poll
  public static Poll get() {return poll;}

  // get current question
  public static Question question()
  {
    return (answers == null) ? q0 : poll.question(answers);
  }

  public static Question onAnswer(Context context, SharedPreferences sp, Listener listener, int option)
  {
    // zero question?
    if (answers == null)
    {
      switch (option)
      {
        case 0: // yes - go to the first question
          answers = "";
          break;
        case 1: // later - will be back tomorrow
          clear();
          return null;
        case 2: // no - submit refusal
          break;
      }
    }
    else
      // append the new answer
      answers += String.valueOf(option);

    // get next question
    Question next = poll.question(answers);

    // is it time to submit?
    if (next == null)
      submit(context, sp, listener);

    return next;
  }

  public static void onCancel() {clear();}

  private static final String LAST_ID = "poll_last_id";

  private static Poll poll = null;
  private static String answers = null;
  private static Question q0;

  private static void submit(Context context, SharedPreferences sp, Listener listener)
  {
    // null in answers means the poll was refused!
    new TaskPollSubmit(listener, Preferences.getDeviceID(context), poll, answers).execute();

    SharedPreferences.Editor editor = sp.edit();

    editor.putLong(LAST_ID, poll.id());
    editor.commit();

    clear();
  }

  private static void clear()
  {
    poll = null;
    answers = null;
  }
}
